home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / util / primitives / synchronization.pyo (.txt) < prev   
Python Compiled Bytecode  |  2008-10-13  |  4KB  |  125 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. from __future__ import with_statement
  5. from itertools import count
  6. import functools
  7. import logging
  8. import os
  9. import threading
  10. import time
  11. log = logging.getLogger('util.primitives.synch')
  12.  
  13. def lock(f):
  14.     
  15.     def wrapper1(instance, *args, **kw):
  16.         if not hasattr(instance, '_lock'):
  17.             
  18.             try:
  19.                 instance._lock = threading.RLock()
  20.             except AttributeError:
  21.                 raise NotImplementedError, '%s needs a _lock slot' % instance.__class__.__name__
  22.             except:
  23.                 None<EXCEPTION MATCH>AttributeError
  24.             
  25.  
  26.         None<EXCEPTION MATCH>AttributeError
  27.         instance._lock.__enter__()
  28.         
  29.         try:
  30.             val = f(instance, *args, **kw)
  31.         finally:
  32.             pass
  33.  
  34.         return val
  35.  
  36.     wrapper1 = (functools.wraps(f),)(wrapper1)
  37.     return wrapper1
  38.  
  39.  
  40. class RepeatCheck(object):
  41.     
  42.     def __init__(self, idfunc = None):
  43.         self.ids = sentinel
  44.         if idfunc is None:
  45.             idfunc = id
  46.         
  47.         self.id = idfunc
  48.  
  49.     
  50.     def __call__(self, *x):
  51.         if x == tuple():
  52.             self.ids = sentinel
  53.             return None
  54.         elif len(x) != 1:
  55.             raise TypeError('takes one argument')
  56.         
  57.         
  58.         try:
  59.             newids = [ self.id(a) for a in x ]
  60.         except TypeError:
  61.             newids = [
  62.                 self.id(a)]
  63.  
  64.         changed = newids != self.ids
  65.         self.ids = newids
  66.         return changed
  67.  
  68.  
  69.  
  70. def repeat_guard(func):
  71.     guard = RepeatCheck()
  72.     
  73.     def wrapper(src, attr, old, new):
  74.         if guard(src):
  75.             return None
  76.         
  77.         return func(src, attr, old, new)
  78.  
  79.     return wrapper
  80.  
  81.  
  82. class HangingThreadDaemon(threading.Thread):
  83.     ids = count()
  84.     
  85.     def __init__(self, wait = 3, sysexit = False):
  86.         threading.Thread.__init__(self, name = 'HangingThreadDaemon %d' % self.ids.next())
  87.         self.wait = wait
  88.         self.sysexit = sysexit
  89.         self.setDaemon(True)
  90.  
  91.     
  92.     def run(self):
  93.         time.sleep(self.wait)
  94.         threads = list(threading.enumerate())
  95.         if threads:
  96.             print 'Remaining non-daemon threads:'
  97.             for thread in threads:
  98.                 if not thread.isDaemon():
  99.                     print ' ', thread
  100.                     continue
  101.             
  102.         
  103.         collect_garbage_and_report()
  104.         if self.sysexit:
  105.             print 'forcing shutdown...'
  106.             os._exit(1)
  107.         
  108.  
  109.  
  110.  
  111. def collect_garbage_and_report():
  112.     import gc as gc
  113.     garbage_count = gc.collect()
  114.     if garbage_count > 0:
  115.         log.info('Garbage collected. ' + str(garbage_count) + ' unreachable objects')
  116.         if garbage_count:
  117.             log.info('Garbage left (only first 20 listed): %r', gc.garbage[:20])
  118.         
  119.     
  120.  
  121. if __name__ == '__main__':
  122.     import doctest
  123.     doctest.testmod(verbose = True)
  124.  
  125.